//
//  SharedExample.swift
//  SwiftCode
//
//  Created by yuhus on 2023/10/16.
//

import UIKit
import Social
import LinkPresentation

class SharedExample: ListViewController {
  
  override func viewDidLoad() {
    super.viewDidLoad()
    title = "文本+视频"
  }
  
  override func getItemCount() -> Int {
    data.count
  }
  
  override func getItem(_ indexPath: IndexPath) -> Item {
    data[indexPath.row]
  }
  
  private lazy var data: [Item] = [
    .init(title: "UIActivityViewController", description: "系统分享", action: systemShare),
    .init(title: "UIActivityViewController", description: "系统分享+Link Presentation", action: systemShareLP),
    .init(title: "UIActivityViewController", description: "系统分享+自定义Activity", action:  systemShareActivity),
    .init(title: "UIActivityViewController", description: "系统分享 自定义Activity", action: systemOnlyCustom),
    .init(title: "UIActivityViewController", description: "系统分享 自定义View", action: systemCustomView),
    .init(title: "SLComposeViewController", description: "Social分享平台", action: socialAvailable),
    .init(title: "SLComposeViewController", description: "Social分享 至 Facebook", action: socialShareToFB),
    .init(title: "SLComposeViewController", description: "Social分享 至 Twitter", action: socialShareToTwitter),
    .init(title: "ShareViewController", description: "自定义分享", action: customShare),
  ]
}

private func systemShare() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4") else { return }
  
  // 预览标题完全显示
  // Facebook 只使用了 #GPark
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  let items: [Any] = [text, url]
  let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
//  activityVC.excludedActivityTypes = [.airDrop]
  activityVC.completionWithItemsHandler = { type, completed, items, error in
    print("分享完成")
  }
  UIViewController.topMost?.present(activityVC, animated: true)
}

private func systemShareLP() {
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  let items: [Any] = [text, ShareItems()] // Facebook 只使用了 #GPark
//  let items: [Any] = [ShareItems()] // 预览 title 为元数据，副标题 originURL，Facebook 只分享视频
  let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
  activityVC.completionWithItemsHandler = { type, completed, items, error in
    print("分享完成")
  }
  UIViewController.topMost?.present(activityVC, animated: true)
}

private func systemShareActivity() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4") else { return }
  
  // 预览标题完全显示
  // Facebook 只使用了 #GPark
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  let items: [Any] = [text, url]
  let activityVC = UIActivityViewController(activityItems: items, applicationActivities: [CustomActicity()])
  activityVC.excludedActivityTypes = UIActivity.ActivityType.allCases
  activityVC.completionWithItemsHandler = { type, completed, items, error in
    print("分享完成")
  }
  UIViewController.topMost?.present(activityVC, animated: true)
}

private struct CustomItem {
  let url: URL
  let text: String
}

private func systemOnlyCustom() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4") else { return }
  
  // 预览标题完全显示
  // Facebook 只使用了 #GPark
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  let customItem = CustomItem(url: url, text: text)
  let items: [Any] = [customItem]
  let activityVC = UIActivityViewController(activityItems: items, applicationActivities: [CustomActicity()])
  //  activityVC.excludedActivityTypes = [.airDrop]
  activityVC.completionWithItemsHandler = { type, completed, items, error in
    print("分享完成")
  }
  UIViewController.topMost?.present(activityVC, animated: true)
}

private func systemCustomView() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4") else { return }
  
  // 预览标题完全显示
  // Facebook 只使用了 #GPark
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  let items: [Any] = [text, url]
  let activityVC = ActivityViewController(activityItems: items, applicationActivities: nil)
  activityVC.completionWithItemsHandler = { type, completed, items, error in
    print("分享完成")
  }
  UIViewController.topMost?.present(activityVC, animated: true)
}

private func socialAvailable() {
  // com.apple.social.twitter
  print("SLServiceTypeTwitter: ", SLComposeViewController.isAvailable(forServiceType: "com.apple.social.twitter"))
  // com.apple.social.facebook
  print("SLServiceTypeFacebook: ", SLComposeViewController.isAvailable(forServiceType: "com.apple.social.facebook"))
  // com.apple.social.sinaweibo
  print("SLServiceTypeSinaWeibo: ", SLComposeViewController.isAvailable(forServiceType: "com.apple.social.sinaweibo"))
  // com.apple.social.tencentweibo
  print("SLServiceTypeTencentWeibo: ", SLComposeViewController.isAvailable(forServiceType: "com.apple.social.tencentweibo"))
//  print("SLServiceTypeLinkedIn: ", SLComposeViewController.isAvailable(forServiceType: SLServiceTypeLinkedIn))
  // 微信
  print("com.tencent.xin.sharetimeline: ", SLComposeViewController.isAvailable(forServiceType: "com.tencent.xin.sharetimeline"))
  print("com.apple.share.Facebook.post: ", SLComposeViewController.isAvailable(forServiceType: "com.apple.share.Facebook.post"))
  print("123456789: ", SLComposeViewController.isAvailable(forServiceType: "123456789"))
}

private func socialShareToFB() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4"),
        let controller = SLComposeViewController(forServiceType: "com.apple.social.facebook") else {
    return
  }
  
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  controller.setInitialText(text)
//  controller.add(Bundle.main.url(forResource: "shareImage", withExtension: "jpeg")!)
  controller.add(url)
  controller.completionHandler = { result in
    print("分享完成", result)
  }
  print("预览弹出")
  UIViewController.topMost?.present(controller, animated: true)
}

private func socialShareToTwitter() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4"),
        let controller = SLComposeViewController(forServiceType: "com.apple.social.twitter") else {
    return
  }
  
  let text = "Ava's video in The Night Fus 2023-07-18 #GPark.."
  controller.setInitialText(text)
//  controller.add(Bundle.main.url(forResource: "shareImage", withExtension: "jpeg")!)
  controller.add(url)
  controller.completionHandler = { result in
    print("分享完成", result)
  }
  print("预览弹出")
  UIViewController.topMost?.present(controller, animated: true)
}

private func customShare() {
  guard let url = Bundle.main.url(forResource: "shareVideo", withExtension: "MP4") else { return }
  UIViewController.topMost?.present(ShareViewController(with: .video(url)), animated: true)
}
